<template>
  <div class="page">
    <div class="login-box">
      <el-form :model='ruleForm' :rules="rules" ref="ruleForm" label-position="left" status-icon>
        <h3 class="title">店铺后台管理系统登陆</h3>
        <el-form-item prop="account">
          <el-input type="text" v-model="ruleForm.account" auto-complete="off" placeholder="账号"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input :type="passType" v-model="ruleForm.password" auto-complete="off" placeholder="密码" id="loginPassword"/>
        </el-form-item>
        <el-form-item prop="code">
          <el-input type="text" v-model="ruleForm.code" auto-complete="off" placeholder="验证码">
            <!--suppress HtmlUnknownTarget -->
            <img class="imageCode" alt="imageCode" slot="append" :src="imageCodeUrl" @click="freshImageCode"/>
          </el-input>
        </el-form-item>
        <el-form-item>
          <label id="showPasswordToggle">
            <el-checkbox v-model="checked" id="showPasswordCheck">显示密码</el-checkbox>
          </label>
          <router-link to="/forget" class="forget">忘记密码？</router-link>
        </el-form-item>
        <el-form-item style="width:100%;">
          <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit" :loading="loginLoading">
            登录
          </el-button>
        </el-form-item>
        <el-form-item style="width:100%;">
          <router-link to="/register">
            <el-button style="width:100%;">
              注册
            </el-button>
          </router-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginByPass } from '@/api/ApiUser'
import { ResultCode } from '@/constants/constants'
import sysConfig from '@/config/sysConfig'
export default {
  name: 'app-login',
  data: () => {
    return {
      loginLoading: false,
      formUrl: '/',
      ruleForm: {
        account: '1393886097@qq.com',
        password: '123123',
        code: ''
      },
      rules: {
        account: [{
          required: true,
          message: '请输入账号',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '验证码不能为空',
          trigger: 'blur'
        }]
      },
      checked: false,
      imageCodeFresh: new Date().getTime()
    }
  },
  computed: {
    passType () {
      return this.checked ? 'text' : 'password'
    },
    imageCodeUrl () {
      return sysConfig.baseUrl + '/middle-user/user/imageCode?key=login&temp=' + this.imageCodeFresh + ')'
    }
  },
  methods: {
    // 登陆
    handleSubmit () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loginLoading = true
          loginByPass(this.ruleForm.account, this.ruleForm.password, this.ruleForm.code, data => {
            if (data && data.code === ResultCode.OK) {
              this.$message.success('登陆成功')
              this.$router.push('/index')
            }
          }, () => {
            this.freshImageCode()
          }, () => {
            this.loginLoading = false
          })
        } else {
          return false
        }
      })
    },
    freshImageCode () {
      this.imageCodeFresh = new Date().getTime()
    }
  },
  // 拦截router，获取上个页面，在登陆成功后返回到原来的页面
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (from.fullPath !== '/register' && !from.meta.errorPage) {
        vm.fromUrl = from.fullPath
      }
    })
  }
}
</script>

<style scoped>

  .title{
    text-align: center;
    margin-bottom: 15px;
  }

  .forget{
    float: right;
    color: #bbbbbb;
  }

  /* 背景页面 */
  .page{
    background-color: #eff3f4;
    position: absolute;
    width: 100%;
    height: 100%;
    font-size: 16px;
    font-family: 'Source Sans Pro', sans-serif;
    font-weight: 400;
    -webkit-font-smoothing: antialiased;
  }

  /* 登陆框 */
  .login-box{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: block;
    width: 100%;
    max-width: 400px;
    background-color: #fff;
    margin: 0;
    padding: 2.25em;
    box-sizing: border-box;
    border: solid 1px #ddd;
    border-radius: .5em;
    font-family: 'Source Sans Pro', sans-serif;
  }

  .imageCode{
    cursor: pointer;
    width: 80px;
    height: 38px;
    display: block;
    margin: 1px;
    background-size: 100% 100%;
  }

</style>
<style>
  /*noinspection CssUnusedSymbol*/
  .el-input-group__append{
    padding: 0;
    border: 12px;
  }
</style>
